<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        ul {
            width: 1200px;
            margin: auto;
            display: flex;
            flex-wrap: wrap;
        }

        li {
            width: 23%;
            margin: 1%;
            box-shadow: 0 0 1px 1px #ddd;
            text-align: center;
        }

        img {
            width: 100%;
            height: 300px;
        }

        .page {
            width: 1200px;
            margin: auto;
        }

        .off {
            color: #ddd;
        }
    </style>
</head>

<body>

    <ul>

    </ul>

    <div class="page">
        <span class="prev off">&lt;</span>
        <strong class="pageAll">共n页</strong>
        <span class="next">&gt;</span>
    </div>


    <script src="../js/ajax.js"></script>
    <script>

        // 分页
        //    总共的数据            数据的总条数(后台)
        //    每一页要显示几条数据   前端
        //    总共有多少页    数据的条数 / 每一页要显示几条数据
        //    返回一些数据   给10条


        // 
        const oUl = document.querySelector('ul');


        // 每一页要显示几条数据
        const num = 3;
        // 第几页
        let page = 1;
        // 总页数  --- 请求成功以后才会赋值
        let total;



        render();



        // 翻页
        const oPage = document.querySelector('.page');
        const oPrev = document.querySelector('.prev');
        const oNext = document.querySelector('.next');
        // 事件委托
        oPage.onclick = function (e) {
            const target = e.target;   // 此处省略兼容????????
            // 下翻页
            if (target.className === 'next') {
                page++;
                if (page >= total) {
                    target.classList.add('off');
                }
                render();

                // 给上翻页清除off
                if (oPrev.classList.contains('off')) {
                    oPrev.classList.remove('off');
                }
                return;
            }
            // 上翻页
            if (target.className === 'prev') {
                page--;
                if (page === 1) {
                    target.classList.add('off');
                }
                render();

                // 给下翻页清除off
                if (oNext.classList.contains('off')) {
                    oNext.classList.remove('off');
                }
            }
        }






        function render() {
            ajax({
                path: `../php/page.php?num=${num}&page=${page}`,
                success: res => {
                    console.log(res);
                    const { status, msg, data } = res;
                    if (status) {
                        let html = '';
                        data.forEach(v => {
                            const { goods_title, goods_des, goods_price, goods_img, goods_id } = v;
                            html += `
                    <li>
                        <a href="./04detail.html?id=${goods_id}">
                            <img src="${goods_img}" alt="">
                            <h3>${goods_title}</h3>
                            <p>${goods_des}</p>
                            <strong>${goods_price}</strong>
                        </a>
                    </li>
                `
                        })
                        oUl.innerHTML = html;



                        // 显示页数
                        const oPageAll = document.querySelector('.pageAll');

                        // 页数不能被整除  ---- 向上取整
                        total = Math.ceil(res.total / num)
                        oPageAll.innerHTML = '总共' + total + '页';




                    } else {
                        alert(msg);
                    }
                }
            })
        }


    </script>

</body>

</html>